package org.example.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.example.bean.RoomBean;
import org.example.mapper.IRoomMapper;
import org.example.service.IRoomService;

import java.util.List;

public class RoomServiceImp extends BaseService implements IRoomService{

    @Override
    public PageInfo<RoomBean> cutFind(Integer pageNum, String roomAddress, String roomStatus, String roomGender, String roomCanIn) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        PageHelper.startPage(pageNum,2);
        PageInfo<RoomBean> pg = PageInfo.of(mapper.cutFind(roomAddress,roomStatus,roomGender,roomCanIn));
        session.close();
        return pg;
    }

    @Override
    public void add(RoomBean roomBean) {

        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomBean1 = mapper.findByAddress(roomBean.getRoomAddress());
        if (roomBean1 == null){
            mapper.add(roomBean);
            session.commit();
        }
        session.close();
    }


    @Override
    public boolean del(Integer roomID) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Integer num = mapper.countRoomStu(roomID);
        if (num == 0){
            mapper.del(roomID);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    public RoomBean findByID(Integer roomID) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomBean = mapper.findByID(roomID);
        session.close();
        return roomBean;
    }

    @Override
    public List<RoomBean> findByGender(String roomGender) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.findByGender(roomGender);
        session.close();
        return list;
    }
}
